package com.xoms.admin.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.admin.model.ProAllotBean;
import com.xoms.admin.model.ProAllotDetailBean;
import com.xoms.admin.model.vo.BatchAllotBean;
import com.xoms.admin.model.vo.ProAllotVo;
import com.xoms.admin.persist.dao.ProAllotDao;
import com.xoms.util.StringUtil;

@Repository
public class ProAllotImpl extends BaseImpl implements ProAllotDao{

	final String NAME_SPACE = NAME_SPACE_HEADER + ".ProAllotMapper";

	@Override
	public int selectCount(String key, String fromId, String toId, String startTime, String endTime) {
		Map<String,Object> params=new HashMap<String,Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if(!StringUtil.isEmpty(fromId)) {
			params.put("fromId", fromId);
		}
		if(!StringUtil.isEmpty(toId)) {
			params.put("toId", toId);
		}
		if(!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		if(!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount",params);
	}

	@Override
	public List<ProAllotBean> selectList(String key, int page, int pageSize, String fromId, String toId, String startTime, String endTime) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if(!StringUtil.isEmpty(fromId)) {
			params.put("fromId", fromId);
		}
		if(!StringUtil.isEmpty(toId)) {
			params.put("toId", toId);
		}
		if(!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		if(!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);
	}

	@Override
	public int selectDetailCount(String key, String aid) {
		Map<String,Object> params=new HashMap<String,Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if (!StringUtil.isEmpty(aid)) {
			params.put("aid", aid);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDetailCount",params);
	}

	@Override
	public List<ProAllotDetailBean> selectDetailList(String key, int page, int pageSize, String aid) {
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if(!StringUtil.isEmpty(aid)) {
			params.put("aid", aid);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDetailList", params);
	}

	@Override
	public int insert(ProAllotBean data) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", data);
	}

	@Override
	public int update(ProAllotBean data) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", data);
	}


	@Override
	public int delDetailByAId(String aid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delDetailByAId", aid);
	}

	@Override
	public int del(String aid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delete",aid);
	}

	@Override
	public ProAllotBean selectOneById(String did) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", did);
	}

	@Override
	public int replaceBatchDetail(List<BatchAllotBean> batch) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("list", batch);
		return sqlSessionTemplate.insert(NAME_SPACE + ".replaceBatchDetail", params);
	}

	@Override
	public String selectMaxAllotNoInYear(int type) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectMaxAllotNoInYear", type);
	}

	@Override
	public int selectDetailCountById(String aid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectDetailCountById", aid);
	}

	@Override
	public List<ProAllotVo> selectNumberByAidInType(String aid, int type) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("aid", aid);
		params.put("type", type);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectNumberByAidInType", params);

	}

}
